**** Set Globals
clear
global home "C:\Users\venator\Dropbox\" // REPLACE WITH LOCATION OF REPLICATION FOLDER
global data "${home}\icflm_replication_archive\Data\ACS"
global output  "${home}\icflm_replication_archive\Output"
***********spruce up ACS data***********


use "$data/usa_00070", clear


//want household heads of head spouses. Also allow for children to permit boomerang migration
keep if relate <=4

keep if age>=22 & age<=40 //age range
keep if bpl<60 //US natives
//drop unnecessary variables
drop raced educd hispand gq hhwt cbserial sample related empstatd ind migrate1d ///
migcounty1 migtype1  region migmet bpld


//clean up and creation of a few simple variables
drop if migrate1 == 4 //abroad last year
*drop migrate1
drop if educ<3 //at least one year of high school
tab educ


//college dummies and continuous experience variables
gen hs_drop = (educ<6)
gen hs = (educ == 6)
gen some_coll = (educ>6 & educ<10)
gen coll = (educ == 10)
gen grad = (educ == 11)

replace educ = educ + 6
gen exp = age- (educ + 6)
gen exp_2 = exp^2
gen exp_3 = exp^3
gen exp_4 = exp^4

//demographic dummies
gen black = (race == 2 & hispan == 0) 
gen hisp = (hispan>0)
gen white = race == 1 & hispan == 0
gen non_wb = (race != 2 & race != 1) | hispan == 1
gen married = (marst<=2)

//nonsensicla values of yngch
replace yngch = -1 if yngch == 99
drop if age-yngch<14 

//employment status
gen emp = 0
replace emp = 1 if empstat == 1

//topcoded hours worked
replace uhrswork = . if uhrswork == 99
*replace uhrswork = . if uhrswork == 0

//drop negative values of income and construct spousal income variable
drop if inctot<0
drop if ftotinc<0
*gen inc_spouse = 0
*replace inc_spouse = ftotinc - inctot if (relate == 1 | relate == 2)
*drop if inc_spouse<0

//handle past migration variable
ren migplac1 statefips_prev
replace statefips_prev = statefip if statefips_prev == 0
gen mig = (statefips_prev!=statefip) //migration dummy
drop if statefips_prev == 110 //kill Puerto Rico

// Create new household X marriage variable
g serial_v2 = serial+ 1*10000000 if relate == 1 | relate == 2 // household head & spouse
replace serial_v2 = serial + 3*10000000 if relate == 3 | relate == 4 // child of household head & in-law

 
// Drop households for which there are multiple married children within the same HH because we cannot id the spouse then
g relate_v2 =relate
replace relate_v2 = 1 if relate == 3
replace relate_v2 = 2 if relate == 4
 duplicates tag serial_v2 year relate_v2, g(dup)
  duplicates tag serial_v2 year relate_v2 married, g(dup_mar)

drop if dup_mar > 0 & married == 1

// Keep only the single women to append back on
preserve
keep if married == 0
keep if sex == 2
save "$data/acs_single_women.dta", replace
restore

drop if married == 0


isid serial_v2 year relate_v2
*** Create data set of male spouse characteristics
g relate_v3 = 1 if relate_v2 == 1 & sex == 1
replace relate_v3 = 1 if relate_v2 == 2 & sex == 1
replace relate_v3 = 2 if relate_v2 == 1 & sex == 2
replace relate_v3 = 2 if relate_v2 == 2 & sex == 2

**
preserve
keep sex age bpl educ uhrswork inctot  hs_drop hs some_coll coll grad exp exp_2 exp_3 exp_4 black hisp emp serial_v2 year relate_v2
keep if sex == 1

renvars  age bpl educ uhrswork inctot  hs_drop hs some_coll coll grad exp exp_2 exp_3 exp_4 black hisp emp, postfix(_sp)
duplicates tag serial_v2 year, g(dup_sex)
drop if dup_sex > 0 // drops non-heterosexual couples
drop dup_sex
save "$data/acs_spouse_char.dta", replace
restore
//Keep only married women
keep if sex == 2
duplicates tag serial_v2 year, g(dup_sex)
drop if dup_sex > 0
merge 1:1 serial_v2 year using "$data/acs_spouse_char.dta"
drop if _merge == 2
drop _merge
append using "$data/acs_single_women.dta"


*****************Code CCC and amenity values for current, previous, and birth state******************
//current state

ren statefip state_fip
g studyyear = year
replace studyyear = 2008 if year <2008
replace studyyear = 2018 if year >2018
merge m:1 state_fip studyyear using "$data/../CCC/state_NCDP_ccprice.dta"
drop if _merge == 2
drop _merge
rename state_fip statefips
merge m:1 statefips using "$data/../Amenities/state_amenities_collapse"
ren statefips temp
drop if _merge == 2 
drop _merge
local amens `"college unemp violent property pop govt_exp s_t_ratio cooling cc_state_std mctoddler"'
foreach var in `amens'{
	ren `var' `var'_current
}

//previous
ren statefips_prev state_fip
replace studyyear = year- 1
replace studyyear = 2008 if studyyear < 2008
replace studyyear = 2018 if studyyear > 2018
merge m:1 state_fip studyyear using "$data/../CCC/state_NCDP_ccprice.dta"
drop if _merge == 2
drop _merge
rename state_fip statefips
merge m:1 statefips using "$data/../Amenities/state_amenities_collapse"
local amens `"college unemp violent property pop govt_exp s_t_ratio cooling cc_state_std mctoddler"'
ren statefips statefips_prev
foreach var in `amens'{
	ren `var' `var'_prev
}
drop if _merge == 2 
drop _merge
//bpl ccc
ren bpl state_fip
replace studyyear = year
replace studyyear = 2008 if year <2008
replace studyyear = 2018 if year >2018
merge m:1 state_fip studyyear using "$data/../CCC/state_NCDP_ccprice.dta"
drop if _merge == 2
drop _merge
rename state_fip statefips
merge m:1 statefips using "$data/../Amenities/state_amenities_collapse"
local amens `"college unemp violent property pop govt_exp s_t_ratio cooling cc_state_std mctoddler"'
ren statefips bpl
ren temp statefips
foreach var in `amens'{
	ren `var' `var'_bpl
}



//indicators for young children, home migration, and living in bpl
//gen baby = (yngch == 0 | yngch == 1 & nchild == 1) //so, looking at women with their first young child
//gen baby = (yngch == 0 | yngch == 1) //so, looking at women with their first young child
gen baby = (yngch == 0 | yngch == 1 | yngch == 2 | yngch == 3 | yngch == 4)
gen preg_first = (yngch == 0  & nchild == 1)
gen home_mig = 0
replace home_mig = 1 if mig & (statefips == bpl | statefips == bpl_sp)
gen wifehome_mig = 0 
replace wifehome_mig = 1 if mig & statefips == bpl
gen hushome_mig = 0 
replace hushome_mig = 1 if mig & statefips == bpl_sp
replace hushome_mig = . if married == 0 | bpl_sp == .
gen same_state = (bpl == statefips | bpl_sp == statefips)
gen same_state_prev = (statefips_prev == bpl | statefips_prev == bpl_sp)
gen wifesame_state = (bpl == statefips )
gen wifesame_state_prev = (statefips_prev == bpl )
gen hussame_state = (bpl_sp == statefips )
replace hussame_state = .  if married == 0 | bpl_sp == .
gen hussame_state_prev = (statefips_prev == bpl_sp )
replace hussame_state_prev = .  if married == 0 | bpl_sp == .
g leavehome_mig = 0
replace leavehome_mig = 1 if mig & (statefips_prev == bpl | statefips_prev == bpl_sp)
replace leavehome_mig = leavehome_mig*100  if leavehome_mig <=1
gen nonhome_mig = 0
replace nonhome_mig = 1 if mig & (statefips != bpl & statefips != bpl_sp)
replace nonhome_mig = nonhome_mig*100  if nonhome_mig <=1
//replace home_mig = 1 if mig & statefips == bpl
//gen same_state = (bpl == statefips)
//gen same_state_prev = (statefips_prev == bpl)

gen age_2 = age^2
replace labforce = labforce - 1
gen full_time = 0
replace full_time = 1 if uhrswork>=30 & labforce

//three-value fertility status variable
gen fertility = 0
replace fertility = 1 if preg_first
replace fertility = 2 if baby & !preg_first




save "$data/acs_base_02132024", replace



















//end of do-file